package com.hmyd.hmyd_android.ui;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.AppBasic;
import com.hmyd.hmyd_android.adapter.InvestmentHistoryListAdapter;
import com.hmyd.hmyd_android.commons.UrlConstant;
import com.hmyd.hmyd_android.dto.InvestRecord;
import com.hmyd.hmyd_android.dto.Page;
import com.hmyd.hmyd_android.view.listview.XListView;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;

@ContentView(R.layout.activity_investment_history)
public class InvestmentHistoryActivity extends BasicListActivity{
	@ViewInject(R.id.home_title)
	public View home_title;
	@ViewInject(R.id.home_title_txt)
	public TextView home_title_txt;
	@ViewInject(R.id.home_right)
	public TextView home_right;
	
	@ViewInject(R.id.list)
	private XListView mList;
	private InvestmentHistoryListAdapter mInestmentHistoryListAdapter;
	
	private final int REFRESH = 1;
	private final int LOADMORE = 2;
	
	protected boolean hasMore = false;
	private String cursorId;
	
	private List<InvestRecord> listData = new ArrayList<InvestRecord>();
	
	private static final String INTENT_TYPE_ID = "itemId";
	
	private String itemId;
	
	@ViewInject(R.id.count)
	private TextView count;
	
	public static Intent getIntent(Context context,String itemId,boolean isBill,String itemRateId){
		Intent intent = new Intent(context, InvestmentHistoryActivity.class);
		intent.putExtra(INTENT_TYPE_ID, itemId);
		intent.putExtra("isBill", isBill);
		intent.putExtra("itemRateId", itemRateId);
		return intent;
	}
	
	@Override  
    protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}
	
	public XListView getXList() {
		return mList;
	}
	
	@Override
	public void onResume() {
		super.onResume();
		
		if (!isLoading && listData.size() == 0) {
			RequestParams params = new RequestParams();
			params.addBodyParameter("itemId", itemId);
			
			if (getIntent().getBooleanExtra("isBill", false)) {
				params.addBodyParameter("itemRateId",getIntent().getStringExtra("itemRateId"));
				onRequest(REFRESH, UrlConstant.BILL_INVESTRECORD, HttpMethod.GET, params);
			}else {
				onRequest(REFRESH, UrlConstant.INVESTRECORD, HttpMethod.GET, params);
			}
		}
	}
	
	@Override
	protected void setParams() {
		home_title_txt.setText(getResources().getString(R.string.investment_investment_record));
		home_title.setOnTouchListener(touch);
		
		itemId = getIntent().getStringExtra(INTENT_TYPE_ID);
		
		mList.setPullRefreshEnable(true); 	// 允许下拉刷新
		mList.setPullLoadEnable(false); 	// 禁止加载更多
		mList.setAutoLoadEnable(false); 	// 禁止自动加载
		mList.setXListViewListener(this);	// 加载监听
		
		mInestmentHistoryListAdapter = new InvestmentHistoryListAdapter(this, listData);
		mList.setAdapter(mInestmentHistoryListAdapter);
	}
	
	@Override
	protected void leftButton() {
		AppBasic.getInstance().finishCurrentActivity();
	}
	
	@Override
	public void onRefresh() {
		super.onRefresh();
		RequestParams params = new RequestParams();
		params.addBodyParameter("itemId", itemId);

		if (getIntent().getBooleanExtra("isBill", false)) {
			params.addBodyParameter("itemRateId",getIntent().getStringExtra("itemRateId"));
			onRequest(REFRESH, UrlConstant.BILL_INVESTRECORD, HttpMethod.GET, params);
		}else {
			onRequest(REFRESH, UrlConstant.INVESTRECORD, HttpMethod.POST, params);
		}
	}

	@Override
	public void onLoadMore() {
		super.onLoadMore();

		RequestParams params = new RequestParams();
		params.addBodyParameter("itemId", itemId);
		params.addBodyParameter("lastCursor", cursorId);
		
		if (getIntent().getBooleanExtra("isBill", false)) {
			params.addBodyParameter("itemRateId",getIntent().getStringExtra("itemRateId"));
			onRequest(LOADMORE, UrlConstant.BILL_INVESTRECORD, HttpMethod.GET, params);
		}else {
			onRequest(LOADMORE, UrlConstant.INVESTRECORD, HttpMethod.POST, params);
		}
	}
	
	public void onPostSuccess(int postId, String result) {
		super.onPostSuccess(postId, result);
		
		Gson gson = new Gson();
		java.lang.reflect.Type type2 = new TypeToken<Page<InvestRecord>>() {}.getType();
		Page<InvestRecord> page = gson.fromJson(result, type2);
		hasMore = page.hasMore;
		
		if(page!=null && page.records!=null){
			count.setText(page.total+"");
			
			switch (postId) {
			case REFRESH:
				listData.clear();
				listData.addAll(page.records);
				break;
			case LOADMORE:
				listData.addAll(page.records);
				break;
			default:
				break;
			}
			
			if(listData.size()>0){
				cursorId = listData.get(listData.size()-1).cursorId;
			}else{
				cursorId = null;
			}
			
			mInestmentHistoryListAdapter.notifyDataSetChanged();
		}
		
		if(getXList()!=null && hasMore){
			getXList().setPullLoadEnable(true);
		}else if(getXList()!=null && !hasMore){
			getXList().setPullLoadEnable(false);
		}
	}

	public void onPostFailure(int postId, String msg) {
		super.onPostFailure(postId, msg);

		if(getXList()!=null && hasMore){
			getXList().setPullLoadEnable(true);
		}else if(getXList()!=null && !hasMore){
			getXList().setPullLoadEnable(false);
		}
	}
}
